package com.webxp.user;

import java.sql.SQLException;

import com.webxp.DB.DBPoolMgr;
import com.webxp.base.BaseObj;
import com.webxp.base.BaseSystem;

/**
 * 用户实体类
 * 
 * @author hdg1988
 * 
 */
public class User extends BaseObj {
	// 日志
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
			.getLogger("User.java");

	// 用户表信息
	private static String tableName = "XP_USER";
	private static String objIdName = "USERID";

	/**
	 * 构造函数
	 */
	public User() {
		super(tableName, objIdName);
	}

	/**
	 * 设置用户名
	 * 
	 * @param _sName
	 */
	public void setUserName(String _sName) {
		this.setAttribute("UserName", _sName);
	}

	/**
	 * 获取用户名
	 * 
	 * @return
	 */
	public String getUserName() {
		return this.getAttributeAsString("UserName");
	}

	/**
	 * 设置用户密码
	 * 
	 * @param _sPassword
	 */
	public void setPassword(String _sPassword) {
		this.setAttribute("password", _sPassword);
	}

	/**
	 * 获取用户密码
	 * 
	 * @return
	 */
	public String getPassword() {
		return this.getAttributeAsString("password");
	}

	/**
	 * 获取某个ID的用户
	 * 
	 * @param _nId
	 * @return
	 */
	public static User findById(int _nId) {
		try {
			return (User) newInstance()._findById(_nId);
		} catch (Exception ex) {
			logger.error("获取ID[" + _nId + "]的用户失败！");
			ex.printStackTrace();
			return null;
		}

	}

	/**
	 * 测试用例
	 * 
	 * @param args
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws Exception
	 */
	public static void main(String[] args) throws SQLException,
			ClassNotFoundException, Exception {
		BaseSystem.init();
		User u = User.newInstance();
		u.setAttribute("Username", "admin");
		u.save();
		System.out.println((String) findById(1).getAttribute("username")
				+ findById(2).getId());
		DBPoolMgr.close();
	}

	/**
	 * 用户实例创建
	 * 
	 * @return
	 */
	public static User newInstance() throws Exception {
		return (User) BaseObj.newInstance(User.class);
	}
}
